package com.yoomiito.app.ui.songduoduo.freegoods

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import android.view.View
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.sdd.SddFreeGoodsData
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.ui.anyview.sdd.SddFreeGoodsListHeaderView
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.RouterUtils
import com.yoomiito.app.utils.Type
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.dialog.ShareSendMoreAdDialog
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.act_sdd_free_goods_list.*
import kotlinx.android.synthetic.main.layout_rcy.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 送多多---赠品列表
 */
class FreeGoodsListActivity:BaseActivity<FreeGoodsListPresenter>() {

    companion object{
        private const val GoodsId = "goodsId"
        private const val ShareId = "shareId"
        fun start(context: Context,goodsId:String="",shareId:String=""){
            val intent = Intent(context,FreeGoodsListActivity::class.java)
            intent.putExtra(GoodsId,goodsId)
            intent.putExtra(ShareId,shareId)
            context.startActivity(intent)
        }
    }

    private var goodsId=""
    private var shareId=""
    private var currentPage = 1
    val adapter = ListGoodsAdapter(null)

    override fun getLayoutId(): Int = R.layout.act_sdd_free_goods_list

    override fun newP(): FreeGoodsListPresenter = FreeGoodsListPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        iv_back_left.setOnClickListener { finish() }
        tv_center.paint.isFakeBoldText = true
        tv_center.text = "赠品列表"
        goodsId = intent.getStringExtra(GoodsId)
        shareId = intent.getStringExtra(ShareId)

        initStatusView()
        initRcy()
        send.visibility = if (App.mAppConfig.isLogin&&UserInfoConfig.getUser().three_role_id.toInt()>= Type.LevelType.SDD_VIP_10) View.VISIBLE else View.GONE
        send.setOnClickListener {
            showLoading()
            p.getSddShareMsg(5)
        }
        requestData()
    }

    fun requestData(){
        p.list(goodsId,shareId,currentPage)
    }

    private fun initStatusView() {
        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.layout_rcy))
                .addType(EmptyType().setEmptyContent("暂无商品"))
                .build()
        frameLayout.addView(mStatusManager.view())
    }

    private fun initRcy() {
//        val lp = layout_rcy.layoutParams as LinearLayout.LayoutParams
//        val value = DensityUtil.dp2px(12f)
//        lp.bottomMargin = value
//        lp.setMargins(value,value,value,value)
        layout_rcy.layoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 2)
        layout_rcy.setBackgroundColor(ResUtil.getColor(R.color.color_f2f2f2))
//        layout_rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(9f),DensityUtil.dp2px(9f),true))
        adapter.addHeaderView(SddFreeGoodsListHeaderView(context))
        layout_rcy.adapter = adapter
        adapter.setOnLoadMoreListener({
            currentPage++
            requestData()
        },layout_rcy)
        adapter.setOnItemClickListener { adapter, view, position ->
            RouterUtils.toGoodsDetail(this, this.adapter.data[position].id,Type.GoodsType.FreeGift)
        }
    }

    fun data(data:SddFreeGoodsData?){
        if (data==null)return
        if (data?.list?.current_page==1){
            adapter.setNewData(data.list.data)
        }else{
            adapter.addData(data?.list?.data!!)
            adapter.loadMoreComplete()
        }
        if (data.list.current_page==data.list.last_page){
            adapter.loadMoreEnd()
        }
        currentPage = data.list.current_page
    }

    fun sddShareMsg(msg: ShareMsg){
        val dialog = ShareSendMoreAdDialog(this,null,msg, ShareSendMoreAdDialog.GoodsListType)
        dialog.setOnShowListener {
            dismissLoading()
        }
        dialog.show()
    }

}